//:C07:WordList2.cpp
// Illustrates istreambuf_iterator and insert iterators.
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <set>
#include <string>
#include "../require.h"
using namespace std;

int main(int argc, char* argv[])
{
    char* fname = "WorldList2.cpp";
    if(argc > 1)
    {
        fname = argv[1];
    }
    ifstream in(fname);
    assure(in, fname);
    istreambuf_iterator<char> p(in), end;
    set<string> wordlist;
    while(p != end)
    {
        string word;
        insert_iterator<string> ii(word, word.begin());
        // Find the first alpha character:
        while(p != end && !isalpha(*p))
        {
            ++p;
        }
        // Copy until the first non-alpha character:
        while(p != end && isalpha(*p))
        {
            *ii++ = *p++;
        }
        if(word.size() != 0)
        {
            wordlist.insert(word);
        }
    }
    // Output results:
    copy(wordlist.begin(), wordlist.end(),
        ostream_iterator<string>(cout, "\n"));
}